package com.android.lovepick;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.lovepick.util.BaseUtils;
import com.android.lovepick.util.ChatList;
import com.android.lovepick.util.ChatListAdapter;
import com.android.lovepick.util.ChatMsg;

public class ChatListActivity extends Activity
{
	private final String CHATLIST_TAG = "ChatListActivity";

	private ListView mListView;
	private List<ChatList> mChatLists = null;
	private ChatListAdapter mAdapter = null;
	private ProgressDialog mDialog = null;
	private Drawable mUserface = null;

	@Override
	public void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		final String newMsgs = this.getIntent().getStringExtra("messages");
		setContentView(R.layout.chatlist);
		mListView = (ListView) this.findViewById(R.id.lv_chatlist);
		mUserface = getResources().getDrawable(R.drawable.ic_launcher);

		mDialog = new ProgressDialog(this);
		mDialog.setTitle("解析数据");
		mDialog.setMessage("正在解析消息列表, 请稍候");
		mDialog.show();
		new Thread(new Runnable()
		{

			public void run()
			{
				// TODO Auto-generated method stub
				mChatLists = getChatListsFromJson(newMsgs);
				mDialog.dismiss();
			}
		}).start();

		if (mChatLists != null)
		{
			mAdapter = new ChatListAdapter(this, mChatLists);
			mListView.setAdapter(mAdapter);
			mListView
					.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
					{

						public void onItemSelected(AdapterView<?> arg0,
								View arg1, int arg2, long arg3)
						{
							// TODO Auto-generated method stub
							String nickName = mChatLists.get(arg2)
									.getNickName();
							List<ChatMsg> chatMsgs = mChatLists.get(arg2)
									.getMsgs();
							Intent intent = new Intent(ChatListActivity.this,
									ChatMsgActivity.class);
							intent.putExtra("nickname", nickName);
							intent.putExtra("chatmsgs", (Serializable) chatMsgs);
							startActivity(intent);
						}

						public void onNothingSelected(AdapterView<?> arg0)
						{
							// TODO Auto-generated method stub

						}
					});
		}
		else
		{
			BaseUtils.showToast(this, "暂时没有新消息!");
		}

	}

	private List<ChatList> getChatListsFromJson(String json)
	{
		Log.i(CHATLIST_TAG, "====json parse====");
		List<ChatList> chatLists = null;
		try
		{
			JSONArray jsonArray = new JSONArray(json);
			chatLists = new ArrayList<ChatList>();
			for (int i = 0; i < jsonArray.length(); i++)
			{
				ChatList chatList = new ChatList();
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String nickName = jsonObject.getString("nickname");
				chatList.setNickName(nickName);
				JSONArray msgArray = jsonObject.getJSONArray("msg_list");
				ArrayList<ChatMsg> msgs = new ArrayList<ChatMsg>();

				JSONObject tmp = null;
				for (int j = 0; j < msgArray.length(); j++)
				{
					tmp = jsonArray.getJSONObject(j);
					ChatMsg chatMsg = new ChatMsg();
					chatMsg.setContent(tmp.getString("message"));
					chatMsg.setDate(tmp.getString("date"));
					chatMsg.setSendOrRcv(true);
					msgs.add(chatMsg);
				}
				String msgSummary = tmp.getString("message");
				String date = tmp.getString("date");
				chatList.setMsgSummary(msgSummary);
				chatList.setDate(date);
				chatList.setMsgs(msgs);
				chatList.setUserface(mUserface);
				chatLists.add(chatList);
			}
		}
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return chatLists;
	}
}
